//
//  FriendListViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/3/31.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "FriendListViewC.h"
#import "UserModel.h"
#import "Util.h"
#import "UserEntity.h"
#import <UIImageView+AFNetworking.h>
#import <MBProgressHUD.h>

@interface FriendListCell : UITableViewCell{

}
@property (weak, nonatomic) IBOutlet UIImageView *ibheaderImg;
@property (weak, nonatomic) IBOutlet UILabel *nickLab;
@property (weak, nonatomic) IBOutlet UILabel *addressLab;
@property (weak, nonatomic) IBOutlet UIView *cellView;
@end

@implementation  FriendListCell
- (void)viewWillDisappear:(BOOL)animated{
    [self viewWillDisappear:YES];
    self.ibheaderImg.layer.cornerRadius = 30;
    self.ibheaderImg.clipsToBounds = YES;
}
@end


@interface FriendListViewC (){
    NSArray *dataArray;
    NSArray *contArray;
}


@end

@implementation FriendListViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title =@"我的好友";
    
    [self getData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getData{
    
    NSString *idNum = [UserModel sharedInstance].currentUser.ID;
    [[UserModel sharedInstance] myfriendListWithID:idNum myfriend:^(id info) {
        NSArray *array = [info copy];
        dataArray = array;
        if (dataArray.count == 0) {
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            HUD.labelText = @"没有数据";
            [HUD hide:YES afterDelay:1.5f];
        }
        [self.tableView reloadData];
    }];

}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 85.f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FriendListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FriendListCellID"
                             forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    UserEntity *useEty = dataArray[indexPath.row];
    cell.nickLab.text = [NSString stringWithFormat:@"%@",useEty.nick];
    cell.addressLab.text = useEty.address;
    NSString *img = [NSString stringWithFormat:@"http://218.93.12.178:8080/hhw-api/%@",
                     useEty.headpic];
    NSURL *url = [NSURL URLWithString:img];
    [cell.ibheaderImg setImageWithURL:url];
    UIButton *phoneBtn = [[UIButton alloc] initWithFrame:
                          CGRectMake(240, 10, 58, 19)];
    [phoneBtn setTitle:@"电话好友" forState:UIControlStateNormal];
    [phoneBtn addTarget:self action:@selector(phoneBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [phoneBtn setBackgroundColor:RGBCOLOR(16, 191, 229)];
    [phoneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    phoneBtn.titleLabel.font = [UIFont systemFontOfSize: 12.0];
    phoneBtn.tag = indexPath.row;
    UIButton *deleteBtn = [[UIButton alloc] initWithFrame:
                          CGRectMake(240, 47, 58, 19)];
    [deleteBtn setTitle:@"解除关系" forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [deleteBtn setBackgroundColor:[UIColor grayColor]];
    [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    deleteBtn.titleLabel.font = [UIFont systemFontOfSize: 12.0];
    deleteBtn.tag = indexPath.row;
    [cell.cellView addSubview:phoneBtn];
    [cell.cellView addSubview:deleteBtn];
    
    return cell;
}

-(void)phoneBtnAction:(id) sender{
    
    UIButton *btn=(UIButton *)sender;
    UserEntity *usEty = [dataArray objectAtIndex:btn.tag];
    NSString *phone = [NSString stringWithFormat:@"tel://%@",usEty.userPhone];
    [[UIApplication sharedApplication] openURL:
     [NSURL URLWithString:phone]];
    
}
-(void)deleteBtnAction:(id) sender{
    UIButton *btn=(UIButton *)sender;
    UserEntity *usEty = [dataArray objectAtIndex:btn.tag];
    NSString *idNum = [NSString stringWithFormat:@"%@",usEty.ID];
    [[UserModel sharedInstance] myfriendDeleteWithID:idNum Delete:^(BOOL successed, unsigned int value) {
        if (successed) {
            [self.tableView reloadData];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
                                  @"提示" message:@"好友关系解除失败"
                                delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    

}

@end
